var OPENID;
var userdata=[];
var parameter = getRequest();
var tvname = parameter['tvname'];//参数名

$(document).ready(function(){
    localStorage.clear();  //清空 local Storage 存放的所有数据

    document.getElementById('userid').focus();
    $.ajax({
        url: strLin + "sysUser/selectAll",
        type: 'post',
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
            userdata=cdata.data;
            // console.log(userdata);
        }
    });
    
    // alert(' 引用成功！ ');
    /*回车事件*/
    $('#userid').bind('keypress', function (event) {
        if (event.keyCode == "13") {
            //需要处理的事情
            getusername();
            // document.getElementById('pass').focus();

        }
    });
    $('#pass').bind('keypress', function (event) {
        if (event.keyCode == "13") {
            //需要处理的事情
            login();
        }
    });

});

function login(){
    var userid=$('#userid').val().toString();
    var pass=$('#pass').val().toString();

    let filteredArray_new = userdata.filter(function(item) {
        return item.sid == userid && item.pwd == pass;
    });

    // console.log(filteredArray_new);

    if (filteredArray_new.length>0){

        if (filteredArray_new[0].touch===1){
            $("#username").val('');
            $("#username").val(filteredArray_new[0].sname);

            localStorage.setItem("usersid", filteredArray_new[0].sid);
            localStorage.setItem("username", filteredArray_new[0].sname);
            localStorage.setItem("pass", filteredArray_new[0].pwd);
            location.replace("Touchscreen/TouchScreenWarranty.html?tvname="+tvname);
        }else{
            $.messager.show({title:'我的消息', msg:'当前登录账号无权限登录！！'});
            // $.messager.alert('提示', '当前登录账号无权限登录！');
            return;
        }



        // getuserqx(filteredArray_new)


        // location.replace("whiteMain.html?username="+ filteredArray_new[0].sid);
    }else{
        $("#username").val('账号或密码错误，请重新录入！');
        $("#pass").val('');
    }
}

// 根据工号获取对应的用户名称
function getusername() {
    var userid=$('#userid').val().toString();

    let filteredArray = userdata.filter(function(item) {
        // 如果age字段的值大于等于30，则将该项添加到过滤后的数组中
        return item.sid == userid;
    });

    // console.log(filteredArray);

    if (filteredArray.length>0){
        $("#username").val('');
        $("#username").val(filteredArray[0].sname);
        document.getElementById('pass').focus();
    }else{
        $("#username").val('无此用户，请重新录入！');
        $("#userid").val('');
        // document.getElementById('username').focus();
    }
}

// 根据工号获取对应的用户名称
function getusernameNew() {
    var userid=$('#userid').val().toString();

    let filteredArray = userdata.filter(function(item) {
        // 如果age字段的值大于等于30，则将该项添加到过滤后的数组中
        return item.sid == userid;
    });

    // console.log(filteredArray);

    if (filteredArray.length>0){
        $("#username").val('');
        $("#username").val(filteredArray[0].sname);
        document.getElementById('pass').focus();
        textnumber=2;
    }else{
        $("#username").val('无此用户，请重新录入！');
        // $("#userid").val('');
        // document.getElementById('username').focus();
    }
}

//获取当前登录用户的权限
function getuserqx(filteredArray_new) {
    $.ajax({
        url: strLin + "customSqlAccount/callProc",
        type: 'post',
        data: JSON.stringify(['usp_getuserqx',filteredArray_new[0].sid]),
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
            let  data=cdata.data;
            // console.log(data);

            localStorage.setItem("usersid", filteredArray_new[0].sid);
            localStorage.setItem("username", filteredArray_new[0].sname);
            localStorage.setItem('userdata', JSON.stringify(data));  //用户权限对象数组

            location.replace("TouchScreenWarranty.html");

        }

    })
}


var textnumber=1;
function settext(val) {
    textnumber=val;
    if (textnumber==2){
        getusername()
    }
}
//键盘录入数据
function settextnumber(val) {
    if (textnumber==1){
        $("#userid").val($("#userid").val() + val);
        getusernameNew()
    }else{
        $("#pass").val($("#pass").val() + val);
    }
}

//清除数字键盘上的值
function cleanupnumber() {
    if (textnumber==1){
        $("#userid").val('');
    }else{
        $("#pass").val('');
    }
}
//退格
function retreatnumber() {
    if (textnumber==1){
        let number=$("#userid").val();
        $('#userid').val(number.substr(0,number.length-1));
    }else{
        let number=$("#pass").val();
        $('#pass').val(number.substr(0,number.length-1));
    }
}
